package com.ktwlsoft.fundmanage.fundFlow.processHandler.FundOutServiceTask;

import java.util.Date;

import com.ktwlsoft.fundmanage.domain.entity.FundInRecord;
import com.ktwlsoft.fundmanage.domain.entity.FundSuperviseAccept;
import com.ktwlsoft.fundmanage.domain.entity.TransferApply;
import com.ktwlsoft.fundmanage.fundFlow.processHandler.BaseServiceTask;
import com.ktwlsoft.fundmanage.payload.IAccountUser;
import com.ktwlsoft.fundmanage.utils.UserUtils;
import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作废划款单
 * @author LanJian
 */
@Service
public class CancelTransferApplyService extends BaseServiceTask {

    @Override
    public void execute(DelegateExecution execution) {
        long transId = (Long) execution.getVariable("transId");
        IAccountUser user = UserUtils.getUser();
        TransferApply apply = fundOutService.findTransferApply(transId);
        // apply.setCancelOperatorId(user.getUserId());
        // apply.setCancelOperator(user.getName());
        apply.cancel(user.getName(), user.getUserId(), new Date());
        apply.cancelHandled();

        FundInRecord fundInRecord = fundInService.findFundInResult(apply.getFundInCode());
        fundInRecord.restore(apply.getAmount());
    
        FundSuperviseAccept fundSuperviseAccept = fundInService.findBusinessByTsCode(apply.getTsCode());
        fundSuperviseAccept.setAmountLeft(fundSuperviseAccept.getAmountLeft().add(fundInRecord.getAmount()));
    }
}
